import com.mit.web.business.proxy.PurchaseProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.LogUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.view.purchase.InquiryEditWindow;
import com.mit.web.view.purchase.InquiryItemInputWindow;
import com.mit.web.vo.purchase.InquiryItemVO;
import com.mit.web.vo.purchase.InquiryTaskVO;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.DataGrid;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.logging.ILogger;
import mx.managers.PopUpManager;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;

[Bindable] private var _task:InquiryTaskVO;
[Bindable] private var _prmyItems:ArrayCollection;
[Bindable] private var _scndItems:ArrayCollection;
private var _proxy:PurchaseProxy = PurchaseProxy.getInstance();
private var _pm:IProgressMonitor;
private var _logger:ILogger;

public function set task(t:InquiryTaskVO):void{
	_task = t;
}

//Load Items
private function loadDetailsHandler(event:FlexEvent):void{
	_logger = LogUtil.getLogger(this);
	
	var id:int = _task.id;
	var rsp:IInvokeResponder = _proxy.getInquiryItems(id);
	rsp.addResultListener(loadResultHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function loadResultHandler(event:ResultEvent):void{
	var items:ArrayCollection = event.result as ArrayCollection;
	_prmyItems = new ArrayCollection();
	_scndItems = new ArrayCollection();
	for each(var item:InquiryItemVO in items){
		if(Constants.PRIMARY == item.fabricType)
			_prmyItems.addItem(item);
		else
			_scndItems.addItem(item);
	}
}

private function doInput(event:ListEvent):void{
	var dg:DataGrid = event.currentTarget as DataGrid;
	var item:InquiryItemVO = dg.selectedItem as InquiryItemVO;
	if(item){
		var window:InquiryItemInputWindow = new InquiryItemInputWindow();
		window.inquiryItem = item;
		window.addEventListener(PopWinCloseEvent.SAVED, editRecordHandler); 
		PopUpManager.addPopUp(window,this,true);
		PopUpManager.centerPopUp(window);
	}
}

private function editRecordHandler(event:PopWinCloseEvent):void{
	//直接用保存后的VO替换，不重新读取数据库
	var savedItem:InquiryItemVO = event.vo as InquiryItemVO;
	if(Constants.PRIMARY == savedItem.fabricType){
		_prmyItems[dg_prmy.selectedIndex] = savedItem;
		dg_prmy.validateNow();
		showMessageTip(event.msg, dg_prmy);
	}else{
		_scndItems[dg_scnd.selectedIndex] = savedItem;
		dg_scnd.validateNow();
		showMessageTip(event.msg, dg_scnd);
	}

}

//Save operation
private function doSave(event:MouseEvent):void{
	_task.buyer = lst_buyer.selectedId;
	if(lst_buyer.selectedIndex > -1)
		_task.buyerName = lst_buyer.selectedItem.label;
	
	var x:int = 0;
	for each(var p:InquiryItemVO in _prmyItems){
		if(p.supplier.id > 0)
			x ++;
	}
	_task.prmyItemCmpltCount = x;
	
	var y :int = 0;
	for each(var s:InquiryItemVO in _scndItems){
		if(s.supplier.id > 0 )
			y++;
	}
	_task.scndItemCmpltCount = y;
	
	var rsp:IInvokeResponder = _proxy.saveInquiryTask(_task);
	rsp.addResultListener(saveResultHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function saveResultHandler(event:ResultEvent):void{
	var rtn:InquiryTaskVO = event.result as InquiryTaskVO;
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.SAVED);
	_task.lastUpdatedTime = rtn.lastUpdatedTime;
	e.vo = _task;
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.saved",[_task.style.code]);
	this.dispatchEvent(e);
	closeWindow(this);
}
